
class OrderListController {
    constructor($rootScope,$scope,$state,$compile,$sce,UserService,AccountService,LotteryService){
        Object.assign(this,{$rootScope,$scope,$state,$compile,$sce,UserService,AccountService,LotteryService});
        this.$rootScope.currentGroup= this.$state.params.cg;
        this.lotTypeList = [];
        this.statusList = [ {value:-9,name:"全部"}, {value:0,name:"已撤单"},
            {value:1,name:"待付款"}, {value:2,name:"处理中"}, {value:3,name:"出票中"},
            {value:4,name:"下单成功"}, {value:5,name:"下单失败" }, {value:6,name:"部份成功" },];
        this.queryParam = {
            status:this.statusList[0]
        };
        this.orderData = [];
        this.init();
    }

    /**
     * 初始化
     */
    init() {
        let self = this;
        let urlParams = self.$state.params;
        //日期时间范围
        laydate.render({
           elem: '#orderListForm_daterange'
           ,range: '→'
       });

        self.LotteryService.getLottyTypes().then(_success).catch(_error);

        function _success(data) {
            self.lotTypeList = data.data.data_list;
            self.lotTypeList.forEach(function (item, index, array) {
                if (urlParams.lot_id && item.lot_id == urlParams.lot_id) {
                    self.queryParam.lotType = item;
                }
            })
        }

        function _error(reason) {
            console.log(JSON.stringify(reason));
        }

        self.statusList.forEach((item, index, array) => {
            if (urlParams.status && urlParams.status == item.value) {
                self.queryParam.status = item;
            }
        });
        //执行查询
        self.search(1);
    }



    /**
     * 查询
     */
    search(pageNo){
        let self  = this;
        let param = {};
        if(self.queryParam.lotType){
            param.lot_id =  self.queryParam.lotType.lot_id;
        }
        if(self.queryParam.status){
            param.status =  self.queryParam.status.value;
        }else{
            param.status = -9;
        }
        if(pageNo == undefined){
            pageNo = 1;
        }

        //console.log($("#orderListForm_daterange")[0].value);
        let dateRangString = $("#orderListForm_daterange")[0].value;
        if(dateRangString){
          let pieces =   dateRangString.split('→');
          let startDateStr = pieces[0];
          let endDateStr = pieces[1];
          param.start_date =startDateStr;
          param.end_date =endDateStr;
        }

        param.daterange =  self.queryParam.daterange1;
        param.page_index=pageNo;
        param.page_size=10;

        //console.log(JSON.stringify(param));
        self.AccountService.getOrderList(param).then(_success).catch(_error);
        function _success(data) {
            // if(data.success==true){
                self.orderData = data.data;
            // }
        }
        function _error(reason) {
            console.log(JSON.stringify(reason));
            // layer.alert(reason&&reason.text?reason.text:"对不起操作失败！",{
            //     title:"错误",
            //     icon: 2,
            //     btn:["知道了"],
            //     btn1:function (index,layerro) {
            //         layer.close(index);
            //     }
            // });
        };
    }

    /**
     * 显示订单详情
     * @param projectId
     */
    showDetail(projectId){
        let self = this;
        let orderDetailHtml = require("./order-detail.html");
        self.AccountService.getOrderDetail(projectId).then(_success).catch(_error);
        function _success(data) {
            self.$scope.orderDtail = data.details;
           let compiledOrderDetailHtml =  self.$compile(orderDetailHtml)(self.$scope); // 编译导入的文件
            layer.open({
                           icon:2,
                           title:"错误！",
                           content:compiledOrderDetailHtml[0],
                           btn:["好的"],
                           yes:function (index,layerno) {
                               layer.close(index);
                           }
                       });
        }
        function _error(reason) {
            console.log(JSON.stringify(reason));
            let type = typeof reason;
            layer.open({
                   icon:2,
                   title:"错误！",
                   content:type=='object'?reason.text:reason,
                   btn:["好的"],
                   yes:function (index,layerno) {
                       layer.close(index);
                   }
               });
        };
    }
}
OrderListController.$inject = ["$rootScope","$scope","$state","$compile","$sce","UserService","AccountService","LotteryService"];

export default OrderListController;